
//JETNUKE

/*
* Copyright (c) 2004-2006 by Andrea Mazzolini [andreamazzolini@users.sourceforge.net]
* http://www.jetnuke.org
* 
* 
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
* 
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* 
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*  
*/

package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.HeadLine;


public class GenericHeadLinesDAO extends AutoKeys implements
		org.jetnuke.dao.HeadLinesDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.HeadLinesModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("HeadLinesModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			HeadLine u = new HeadLine();
			fillHeadLineFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	protected void fillHeadLineFromResultSet(HeadLine u, ResultSet rs) throws Exception{
		u.setHEADLINESURL(rs.getString("HEADLINESURL"));
		u.setHID(rs.getInt("HID"));
		u.setSITENAME(rs.getString("SITENAME"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.HeadLinesModel#retrieveByPK(int)
	 */
	public HeadLine retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("HeadLinesModel.selectByID"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		HeadLine s = new HeadLine();
		
		if (rs.next()) {
			fillHeadLineFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.HeadLinesModel#deleteByPK(int)
	 */
	public void deleteByPK(int i) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("HeadLinesModel.deleteByID"));
		p.setInt(1,i);
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.HeadLinesModel#update(org.jetnuke.entities.HeadLine)
	 */
	public void update(HeadLine hl) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("HeadLinesModel.update"));
		p.setString(1,hl.getHEADLINESURL());
		p.setString(2,hl.getSITENAME());
		p.setInt(3,hl.getHID());
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.HeadLinesModel#addNew(org.jetnuke.entities.HeadLine)
	 */
	public int addNew(HeadLine hl) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("HeadLinesModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("HeadLinesModel.lastGeneratedHeadlineID"));
		initHeadline(hl, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;

	}

	/**
	 * @param hl
	 * @param p
	 */
	protected void initHeadline(HeadLine hl, PreparedStatement p) throws Exception{
		p.setString(1,hl.getHEADLINESURL());
		p.setString(2,hl.getSITENAME());
		
	}

}
